from django.db import models
from django.db.models import Manager
from django.core.files.storage import FileSystemStorage


# Create your models here.
class UserInfoManage(Manager):
    """
    自定义模型管理类，创建一个保存数据的方法
    """

    def get_queryset(self):
        """相当于过滤掉逻辑删除的字段"""
        return super(UserInfoManage, self).get_queryset().filter(is_delete=False)

    def create(self, username, password, nickname, gender, sign, pic_):
        u = self.model()
        u.username = username
        u.password = password
        u.nickname = nickname
        u.gender = gender
        u.sign = sign
        u.is_delete = False
        f = FileSystemStorage()  # 保存图片
        path = f.save("pic/{}".format(pic_.name), pic_)
        u.pic = path

        return u


class UserInfo(models.Model):
    """
    主要字段：
     用户名，昵称，性别，密码，注册时间，个性签名，头像
    """
    username = models.CharField('用户名', max_length=150, unique=True)

    nickname = models.CharField('昵称', max_length=150, blank=True)

    gender = models.CharField('性别', default="男", max_length=1, blank=True)

    password = models.CharField('密码', max_length=150)

    create_time = models.DateTimeField('注册时间', auto_now_add=True)  # auto_now_add 自定添加创建时间

    sign = models.CharField('个性签名', max_length=500, blank=True)

    pic = models.ImageField(upload_to="pic/", default="pic/pic.jpg")  # upload_to 图片保存的路径， default 设置默认头像

    is_delete = models.BooleanField(default=False)

    objects = UserInfoManage()

    class Meta:
        verbose_name_plural = '用户信息'

    def __str__(self):
        return self.username
